package src.com.unapec.seguridad.EjemploFinal;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class BDManager {

	private SecurityConnection sc = new SecurityConnection(); 

	
	public ArrayList<String> getUsers(){
		
		ArrayList<String> users = new ArrayList<String>();
		
		try {

			sc.connect();
			Statement statement = sc.getConnection().createStatement();
			ResultSet rc = statement.executeQuery("select id from Login;");

			while(rc.next()){
				
				users.add(rc.getString("id"));
				
			}
			
			
			
		} catch (SQLException e) {

			e.printStackTrace();

		}

		sc.disconnect();
		return users;

	}

	
	
	
	public void write(String username, String password){

		try {

			sc.connect();
			Statement statement = sc.getConnection().createStatement();
			statement.execute("Update Login set password="+"\'"+password+"\' where id = "+"\'"+username+"\';");

		} catch (SQLException e) {

			e.printStackTrace();

		}

		sc.disconnect();

	}
	
	public int getCount(){
		
		int counter = 0;
		
		try {

			sc.connect();
			Statement statement = sc.getConnection().createStatement();
			ResultSet rs = statement.executeQuery("Select count(*) from Login;");
			
			while(rs.next()){
			
			counter = rs.getInt("count(*)");
			
			}
			
			
		} catch (SQLException e) {

			e.printStackTrace();

		}

		sc.disconnect();
		return counter;

		
		
	}

	public String getPassword(String username, String confirmationNumber){

		String password = null;
		
		try {

			sc.connect();
			Statement statement = sc.getConnection().createStatement();
			ResultSet rs = statement.executeQuery("Select password from Login where id = "+"\'"+username+"\' and confirmation ="+"\'"+confirmationNumber+"\';");
			
			while(rs.next()){
			
			password = rs.getString("password");
			
			}
			
			
		} catch (SQLException e) {

			e.printStackTrace();

		}

		sc.disconnect();
		return password;
		
	}
	
	public ResultSet getData(){
		
		ResultSet rs = null;
		ArrayList<String> data = new ArrayList<String>();
		
		try {

			sc.connect();
			Statement statement = sc.getConnection().createStatement();
			rs = statement.executeQuery("Select * from Login");
			
			
		} catch (SQLException e) {

			e.printStackTrace();

		}

		sc.disconnect();
		return rs;

		
	}

	



}
